package com.epin.business.commons.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.epin.base.plugins.AbstractBean;
import com.epin.base.plugins.BaseController;
import com.epin.business.commons.entity.bo.ForeignKeyNormal;
import com.epin.business.commons.entity.vo.ForeignKeyParam;
import com.epin.business.commons.service.ForeignKeyService;
import com.epin.business.system.entity.Role;
import com.epin.util.SessionUtil;
import com.feilong.core.Validator;

/**
 * <p>
 * 表伪外键记录表  前端控制器
 * </p>
 *
 * @author fujiajn
 * @since 2017-09-08
 */
@Controller
@RequestMapping("/foreignKey/")
public class ForeignKeyController extends BaseController {
	
	private final static String NOT_ADMIN_ERROR = "不是超级管理员不允许操作";
	
	@Autowired
	private ForeignKeyService foreignKeyService;
	
	
	/**
	 * <p>跳转主页面</p>
	 * 
	 */
	@RequestMapping(value = "listUI", method = RequestMethod.GET)
    public String listUI() {
		
		// 判断是否为超级管理员
		Role role = SessionUtil.getCurrentUser().getRole();
		if (!ADMIN_ROLE_KEY.equals(role.getKey())) {
			return "403";
		}
		
		return "business/commons/foreignkey/list";
    }
	
	
	/**
	 * <p>跳转新增页面</p>
	 * 
	 */
	@RequestMapping(value = "form", method = RequestMethod.GET)
	public String form(Map<String, Object> map) {
		
		// 判断是否为超级管理员
		Role role = SessionUtil.getCurrentUser().getRole();
		if (!ADMIN_ROLE_KEY.equals(role.getKey())) {
			return "403";
		}
		
		return "business/commons/foreignkey/form";
    }
	
	
	/**
	 * <p>数据加载</p>
	 * 
	 */
	@RequestMapping(value = "load", method = RequestMethod.GET)
    @ResponseBody
    public Object load(Integer pageNumber, Integer pageSize, String keyword) {
		
		Map<String, Object> result = new HashMap<>();
		
		// 判断是否为超级管理员
		Role role = SessionUtil.getCurrentUser().getRole();
		if (!ADMIN_ROLE_KEY.equals(role.getKey())) {
			return result;
		}
		
    	// 添加参数
    	Map<String, Object> params = new HashMap<>();
    	params.put("keyword", keyword.trim());
    	
    	// 获取分页
		Page<ForeignKeyNormal> list = foreignKeyService.selectForeignKeysPage(pageNumber, pageSize, params);
		
		result.put("total", list.getTotal());
		result.put("rows", list.getRecords());
		result.put("pageNumber", pageNumber);
		
		return result;
    }
	
	
	/**
	 * <p>保存方法</p>
	 * 
	 */
	@RequestMapping(value = "save", method = RequestMethod.POST)
	@ResponseBody
	public AbstractBean save(ForeignKeyParam fParam){
		
		// 判断是否为超级管理员
		Role role = SessionUtil.getCurrentUser().getRole();
		if (!ADMIN_ROLE_KEY.equals(role.getKey())) {
			return fail(NOT_ADMIN_ERROR);
		}
		
		return result(foreignKeyService.insertForeignKey(fParam, SessionUtil.getCurrentUser()));
	}
	
	
	/**
	 * <p>删除方法</p>
	 * 
	 */
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
	public AbstractBean delete(Integer[] ids){
		
		// 判断是否为超级管理员
		Role role = SessionUtil.getCurrentUser().getRole();
		if (!ADMIN_ROLE_KEY.equals(role.getKey())) {
			return fail(NOT_ADMIN_ERROR);
		}
		
		if(Validator.isNullOrEmpty(ids) || ids.length == 0){
			return fail("没有选择要删除的行");
		}
		
		return result(foreignKeyService.deleteForeignKeysByIds(Arrays.asList(ids)));
	}
}
